<?php
/*
Plugin Name: yafei-rest 插件
Description: 亚非自用rest
Plugin URI: http://www.oyfei.cn/
Author: yafei
Author URI: http://www.oyfei.cn/
Version: 0.1
Plugin URI: http://www.oyfei.cn/
*/

/* 文章中加入缩略图 */
//add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 );
//
//function my_rest_prepare_post( $data, $post, $request ) {
//    $_data = $data->data;
//
//    if ( has_post_thumbnail() ) {
//        $thumbnail_id = get_post_thumbnail_id( $_data['id'] );
//        $thumbnail = wp_get_attachment_image_src( $thumbnail_id , 'thumbnail' );
//        $featuredimg = wp_get_attachment_image_src( $thumbnail_id , 'full' );
//        $thumbnailurl = $thumbnail[0];
//        $featuredimgurl = $featuredimg[0];
//        if( ! empty($thumbnailurl)){
//            $_data['thumbnailurl'] = $thumbnailurl;
//        }
//        if( ! empty($featuredimgurl)){
//            $_data['featuredimgurl'] = $featuredimgurl;
//        }
//    }else{
//        $_data['thumbnailurl'] = null;
//        $_data['featuredimgurl'] = null;
//    }
//
//    $pure_post = array();
//    $pure_post['id'] = $_data['id'];
//    $pure_post['title'] = $_data['title']['rendered'];
//    $pure_post['date'] = $_data['date'];
//    $pure_post['thumbnailurl'] = $_data['thumbnailurl'];
//
//    $params = $request->get_params();
//    if ( isset( $params['id'] ) ) {
//
//        //增加分类字段
//        $categories = my_rest_get_post_terms( $_data['id'], 'category' );
//        if ( ! empty( $categories ) ) {
//            $_data['categories'] = $categories;
//        }
//        //增加标签字段
//        $tags = my_rest_get_post_terms( $_data['id'], 'post_tag' );
//        if ( ! empty( $tags ) ) {
//            $_data['tags'] = $tags;
//        }
//
//        $pure_post['categories'] = $_data['categories'];
//        $pure_post['tags'] = $_data['tags'];
//        $pure_post['featuredimgurl'] = $_data['featuredimgurl'];
//        $pure_post['content'] = $_data['content']['rendered'];
//
//    }
//
//    return $pure_post;
//
//}
//
///*获取分类和标签*/
//function my_rest_get_post_terms( $id = false, $taxonomy = 'category' ) {
//    if ( ! $id ) {
//        return FALSE;
//    }
//    $valid_tax = apply_filters( 'my_rest_valid_tax', array( 'category', 'post_tag' ) );
//    $taxonomy = ( in_array( $taxonomy, $valid_tax ) ) ? $taxonomy : 'category';
//    $terms = wp_get_post_terms( absint( $id ), $taxonomy );
//    if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
//        foreach ( $terms as $term ) {
//            $link = get_term_link( $term );
//            $term->link = $link;
//        }
//    }
//    return $terms;
//}


add_action( 'rest_api_init', 'rest_post_thumbnail');

function rest_post_thumbnail()
{
    register_rest_field(
        'post',
        'thumbnailurl',
        [
            'get_callback' => 'get_rest_post_thumbnail',
            'update_callback' => null,
            'schema'    => null
        ]
    );
}

function get_rest_post_thumbnail( $object, $field_name, $request )
{
    return get_the_post_thumbnail_url( $object['id']);
}



add_action( 'rest_api_init', 'rest_product_thumbnail');

function rest_product_thumbnail()
{
    register_rest_field(
        'product',
        'thumbnailurl',
        [
            'get_callback' => 'get_rest_product_thumbnail',
            'update_callback' => null,
            'schema'    => null
        ]
    );
}

function get_rest_product_thumbnail( $object, $field_name, $request )
{
    return get_the_post_thumbnail_url( $object['id']);
}











/**
 * Register a book post type, with REST API support
 *
 * Based on example at: http://codex.wordpress.org/Function_Reference/register_post_type
 */
//add_action( 'init', 'my_book_cpt' );
function my_book_cpt() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'your-plugin-textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'show_in_rest'       => true,
        'rest_base'          => 'books-api',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'book', $args );
}


/**
 * Register a genre post type, with REST API support
 *
 * Based on example at: https://codex.wordpress.org/Function_Reference/register_taxonomy
 */
//add_action( 'init', 'my_book_taxonomy', 30 );
function my_book_taxonomy() {

    $labels = array(
        'name'              => _x( 'Genres', 'taxonomy general name' ),
        'singular_name'     => _x( 'Genre', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Genres' ),
        'all_items'         => __( 'All Genres' ),
        'parent_item'       => __( 'Parent Genre' ),
        'parent_item_colon' => __( 'Parent Genre:' ),
        'edit_item'         => __( 'Edit Genre' ),
        'update_item'       => __( 'Update Genre' ),
        'add_new_item'      => __( 'Add New Genre' ),
        'new_item_name'     => __( 'New Genre Name' ),
        'menu_name'         => __( 'Genre' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'genre' ),
        'show_in_rest'       => true,
        'rest_base'          => 'genre',
        'rest_controller_class' => 'WP_REST_Terms_Controller',
    );

    register_taxonomy( 'genre', array( 'book' ), $args );

}


















//自定义缩略图尺寸
add_image_size('news_medium',632,638,true);

/*
 * 增加news的特色图像
 */
add_action( 'rest_api_init', 'rest_news_thumbnail');
function rest_news_thumbnail()
{
    register_rest_field(
        'news',
        'thumbnailurl',
        [
            'get_callback' => 'get_rest_news_thumbnail',
            'update_callback' => null,
            'schema'    => null
        ]
    );
}

function get_rest_news_thumbnail( $object, $field_name, $request )
{
//    return get_the_post_thumbnail_url( $object['id'],'news_medium');
    return get_the_post_thumbnail_url( $object['id']);
}


/*
 * 增加news的特色图像
 */
add_action( 'rest_api_init', 'rest_news_thumbnail');
function rest_diseases_thumbnail()
{
    register_rest_field(
        'diseases',
        'thumbnailurl',
        [
            'get_callback' => 'get_rest_diseases_thumbnail',
            'update_callback' => null,
            'schema'    => null
        ]
    );
}

function get_rest_diseases_thumbnail( $object, $field_name, $request )
{
//    return get_the_post_thumbnail_url( $object['id'],'news_medium');
    return get_the_post_thumbnail_url( $object['id']);
}



//创建科普栏目
add_action( 'init', 'yafei_custom_init_news' );
add_action( 'init', 'yafei_taxonomies_news', 0 );

//创建疾病栏目
add_action( 'init', 'yafei_custom_init_diseases' );
add_action( 'init', 'yafei_taxonomies_diseases', 1 );

//创建医生内容栏目
add_action( 'init', 'yafei_custom_init_doctors' );
add_action( 'init', 'yafei_taxonomies_doctors', 2 );

//创建圈子栏目
add_action( 'init', 'yafei_custom_init_groups' );
add_action( 'init', 'yafei_taxonomies_groups', 2 );


/*
 * 定义科普栏目
 */

function yafei_custom_init_news() {

    $supports = [
        'title',
        'editor',
        'thumbnail',
        'excerpt',
        //'custom-fields',
        'revisions',
        'post-formats'
    ];

    $args = array(
        'public' => true,
        'label'  => '科普',
        'supports'=> $supports,
        'show_in_rest'=> true,
        //'taxonomies'=> $taxonomies
    );
    register_post_type( 'news', $args );
}

/*
 * 定义科普分类
 */
function yafei_taxonomies_news() {
    $labels = [
        'name'              => _x( '科普分类', 'taxonomy 名称' ),
        'singular_name'     => _x( '科普分类', 'taxonomy 单数名称' ),
        'search_items'      => __( '搜索科普分类' ),
        'all_items'         => __( '所有科普分类' ),
        'parent_item'       => __( '该科普分类的上级分类' ),
        'parent_item_colon' => __( '该科普分类的上级分类：' ),
        'edit_item'         => __( '编辑科普分类' ),
        'update_item'       => __( '更新科普分类' ),
        'add_new_item'      => __( '添加新的科普分类' ),
        'new_item_name'     => __( '新科普分类' ),
        'menu_name'         => __( '科普分类' ),
    ];
    $args = [
        'labels' => $labels,
        'hierarchical' => true,
        'show_in_rest' => true,
    ];

    register_taxonomy( 'news_category', 'news', $args );
}


/*
 * 定义疾病栏目
 */

function yafei_custom_init_diseases() {

    $supports = [
        'title',
        'editor',
        'thumbnail',
        'excerpt',
        'custom-fields',
        'revisions'
    ];

    $args = array(
        'public' => true,
        'label'  => '疾病管理',
        'supports'=> $supports,
        'show_in_rest'=> true,
        //'taxonomies'=> $taxonomies
    );
    register_post_type( 'diseases', $args );
}

/*
 * 定义疾病分类
 */
function yafei_taxonomies_diseases() {
    $labels = [
        'name'              => _x( '疾病分类', 'taxonomy 名称' ),
        'singular_name'     => _x( '疾病分类', 'taxonomy 单数名称' ),
        'search_items'      => __( '搜索疾病分类' ),
        'all_items'         => __( '所有疾病分类' ),
        'parent_item'       => __( '该疾病分类的上级分类' ),
        'parent_item_colon' => __( '该疾病分类的上级分类：' ),
        'edit_item'         => __( '编辑疾病分类' ),
        'update_item'       => __( '更新疾病分类' ),
        'add_new_item'      => __( '添加新的疾病分类' ),
        'new_item_name'     => __( '新疾病分类' ),
        'menu_name'         => __( '疾病分类' ),
    ];
    $args = [
        'labels' => $labels,
        'hierarchical' => true,
        'show_in_rest'       => true,
    ];
    register_taxonomy( 'diseases_category', 'diseases', $args );
}



/*
 * 定义医生栏目
 */

function yafei_custom_init_doctors() {

    $supports = [
        'title',
        'editor',
        'thumbnail',
        'excerpt',
        'custom-fields',
        'revisions'
    ];

    $args = array(
        'public' => true,
        'label'  => '医生列表',
        'supports'=> $supports,
        'show_in_rest'=> true,
        //'taxonomies'=> $taxonomies
    );
    register_post_type( 'doctors', $args );
}

/*
 * 定义疾病分类
 */
function yafei_taxonomies_doctors() {
    $labels = [
        'name'              => _x( '医生分类', 'taxonomy 名称' ),
        'singular_name'     => _x( '医生分类', 'taxonomy 单数名称' ),
        'search_items'      => __( '搜索医生分类' ),
        'all_items'         => __( '所有医生分类' ),
        'parent_item'       => __( '该医生分类的上级分类' ),
        'parent_item_colon' => __( '该医生分类的上级分类：' ),
        'edit_item'         => __( '编辑医生分类' ),
        'update_item'       => __( '更新医生分类' ),
        'add_new_item'      => __( '添加新的医生分类' ),
        'new_item_name'     => __( '新医生分类' ),
        'menu_name'         => __( '医生分类' ),
    ];
    $args = [
        'labels' => $labels,
        'hierarchical' => true,
        'show_in_rest'       => true,
    ];
    register_taxonomy( 'doctors_category', 'doctors', $args );
}


/*
 * 定义圈子栏目
 */

function yafei_custom_init_groups() {

    $supports = [
        'title',
        'editor',
        'thumbnail',
        'excerpt',
        'custom-fields',
        'revisions',
        'post-formats'
    ];

    $args = array(
        'public' => true,
        'label'  => '圈子',
        'supports'=> $supports,
        'show_in_rest'=> true,
        //'taxonomies'=> $taxonomies
    );
    register_post_type( 'groups', $args );
}

/*
 * 定义圈子分类
 */
function yafei_taxonomies_groups() {
    $labels = [
        'name'              => _x( '圈子分类', 'taxonomy 名称' ),
        'singular_name'     => _x( '圈子分类', 'taxonomy 单数名称' ),
        'search_items'      => __( '搜索圈子分类' ),
        'all_items'         => __( '所有圈子分类' ),
        'parent_item'       => __( '该圈子分类的上级分类' ),
        'parent_item_colon' => __( '该圈子分类的上级分类：' ),
        'edit_item'         => __( '编辑圈子分类' ),
        'update_item'       => __( '更新圈子分类' ),
        'add_new_item'      => __( '添加新的圈子分类' ),
        'new_item_name'     => __( '新圈子分类' ),
        'menu_name'         => __( '圈子分类' ),
    ];
    $args = [
        'labels' => $labels,
        'hierarchical' => true,
        'show_in_rest'       => true,

    ];
    register_taxonomy( 'groups_category', 'groups', $args );
}

